package com.ebt.m.account.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.account.AccountServiceManager;
import com.ebt.m.account.AccountConstant;
import com.ebt.m.utils.StringUtils;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.utils.os.HandlerBase;
import com.ebt.m.widget.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建时间：2014-12-23 下午2:22:57
 * 项目名称：EBT.app
 *
 * @author allen.li
 * @version 1.0
 * @since JDK 1.6.0_21
 * 文件名称：ActForgetPwdGetAccount.java
 * 类说明：
 */
public class ActForgetPwdGetAccount extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.center_textview)
    TextView title;

    @BindView(R.id.btn_next)
    Button btn_next;

    @BindView(R.id.et_account)
    EditText et_account;

    String account = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd_account_confirm);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    public void initView() {
        title.setText("账号确认");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lib_base_header_bar_left:
                finish();
                break;
        }
    }

    public void initData() {
        Bundle it = getIntent().getExtras();
        if (it != null) {
            String userName = it.getString(AccountConstant.USER_NAME);
            if (!StringUtils.isEmpty(userName)) {
                et_account.setText(userName);
            }
        }
    }

    @OnClick(R.id.btn_next)
    public void gotoChooseContact(View v) {
        account = getStr(et_account);
        if (StringUtils.isEmpty(account)) {
            toast("请输入账号。");
            return;
        } else {
            UIHelper.hideSoftInputFromWindow(this);
            checkAccountExist(account);
        }
    }

    final Handler handlerGetBindPhoneOrEmail = new HandlerBase(AppContext.getInstance()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dismissProgressDialog();
            switch (msg.what) {
                case AccountServiceManager.MSG_ACCOUNT_OK:
                    String[] phone_email = (String[]) msg.obj;
                    String phone = phone_email[0];
                    if (StringUtils.isEmpty(phone)) {
                        toast("账号不存在");
                    } else {
                        Bundle bd = new Bundle();
                        bd.putString(AccountConstant.USER_NAME, account);
                        bd.putString("phone", phone);
                        gotoActivity(ActForgetPwdChooseContact.class, bd);
                    }
                    break;
                default:
                    toast("账号不存在");
                    break;
            }
        }
    };

    private void checkAccountExist(String account) {
        showProgressDialog("请稍后", "正在验证账号...");
        new Thread() {
            public void run() {
                new AccountServiceManager().getBindPhoneOrEmailByUserName(account,
                        handlerGetBindPhoneOrEmail);
            }
        }.start();
    }

    @OnClick(R.id.return_button)
    public void btnCancel(View v) {
        finish();
    }
}
